package com.example.tijianserver.service;

import com.example.tijianserver.dto.PersonalOrderDTO;
import com.example.tijianserver.dto.ReportListDTO;
import com.example.tijianserver.mapper.HospitalMapper;
import com.example.tijianserver.mapper.OrdersMapper;
import com.example.tijianserver.mapper.SetmealMapper;
import com.example.tijianserver.pojo.Hospital;
import com.example.tijianserver.pojo.Orders;
import com.example.tijianserver.pojo.Setmeal;
import com.example.tijianserver.response.ApiRestResponse;
import com.example.tijianserver.response.TijianException;
import com.example.tijianserver.response.TijianExceptionEnum;
import com.example.tijianserver.utils.DateFormatExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService{

    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    HospitalMapper hospitalMapper;


    @Override
    public Orders insertSelective(Orders orders) {
        return ordersMapper.insertSelective(orders);
    }

    //取消订单
    @Override
    public int updatestate(int orderId, int state) {
        int f=ordersMapper.updatestate(orderId,state);
        if (f>0){
            return 200;
        }else {
            throw new TijianException(TijianExceptionEnum.SYSTEM_ERROR);//插入失败
        }

    }

    //orders插入
    @Override
    public int insert(Orders orders) {
        return ordersMapper.insert(orders);
    }

    //根据userId获取订单信息
    @Override
    public List<Orders> getPersonalOrder(String userId) {
        return  ordersMapper.getPersonalOrder(userId);
    }

    //
    @Override
    public List<PersonalOrderDTO> getPOrder(String userId) throws ParseException {

        List<Orders> order = ordersMapper.getPersonalOrder(userId);

        List<PersonalOrderDTO> porders = new ArrayList<>();

        for (Orders orders:order) {
            Setmeal setmeal = setmealMapper.selectByPrimaryKey(orders.getSmId());
            PersonalOrderDTO p = new PersonalOrderDTO();
            p.setOrderId(orders.getOrderId());
            //将时间格式CST转换为GMT
            Date date = DateFormatExample.getDate(orders.getOrderDate());
            p.setOrderDate(date);

            p.setSetmealName(setmeal.getName());
            p.setState(orders.getState());

            porders.add(p);
        }
        return porders;
    }

    //用户已体检订单列表
    @Override
    public List<ReportListDTO> getReport(Orders orders) {
        List<Orders> order = ordersMapper.getPersonalOrder(orders.getUserId());
        List<ReportListDTO> reportlist = new ArrayList<>();

        // 遍历订单列表
        for (Orders order1 : order) {
            // 只处理状态为 3 的订单(已归档)
            if (order1.getState() == 3) {

                //获取医院信息
                Integer hpId = order1.getHpId();
                Hospital hp = hospitalMapper.selectByPrimaryKey(hpId);

                ReportListDTO reportListDTO = new ReportListDTO();

                reportListDTO.setUserId(order1.getUserId());
                reportListDTO.setOrderId(order1.getOrderId());
                reportListDTO.setHpId(order1.getHpId());
                reportListDTO.setHospitalName(hp.getName());
                reportListDTO.setSmId(order1.getSmId());
                reportListDTO.setOrderDate(order1.getOrderDate());

                reportlist.add(reportListDTO); // 将符合条件的订单加入新列表
            }

        }
        return reportlist;
    }

}
